﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
namespace WPFExcelReport.ViweModels
{
    public class WorkFlowDocumentViewModel:BaseFunction
    {
      public  ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        private Dictionary<string, object> dicParamater
        {
            get;
            set;
        }
        public string Mandant
        {
            get
            {
                return app.Mandant;
            }
        }
        public string company
        {
            get
            {
                return app.company;
            }
        }
        public string UserID
        {
            get
            {
                return app.User;
            }
            set
            {
                app.User = value;
            }
        }
        bool IsSetEnvUpdate = true;
        public SETINTEXDB SetEnv
        {
            get;
            set;

        }
        public SETINTEXDB SetDetail
        {
            get;
            set;
        }
 
        public SETINTEXDB SetMaster
        {
            get;
            set;
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public WorkFlowDocumentViewModel()
        {
            if (SetEnv == null)
            {
                SetEnv = new SETINTEXDB();
            }
            if (SetMaster == null)
            {
                SetMaster = new SETINTEXDB();
            }
            if (SetDetail == null)
            {
                SetDetail = new SETINTEXDB();
            }
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public override DataSet LogIn(string MANDANT, string UsingID, System.Security.SecureString sPassword)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult;
            DataTable tableLocal = null;
            OracleCommand or1;
            try
            {
                IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(sPassword);
                string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
                UsingID = UsingID.Trim().ToUpper();
                Password = Password.Trim().ToUpper();
                DataRow dwTemp =SetEnv.HRSTAMM.NewRow();
                dwTemp["HRMANDANT"] = MANDANT;
                dwTemp["HRBENID"] = Left(UsingID + "                    ", 8);
                dwTemp["HRDFAX"] = Left(Password + "                    ", 20);
                SResult = INTEXDBC.QueryHRSTAMM(dwTemp, 1);
                SReturn.Merge(SResult);
                
                if (IsSetEnvUpdate)
                {
                    SetEnv.Clear();
                    SetEnv.Merge(LoadEnv(UsingID));
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return SReturn;
        }
        public DataSet LoadEnv(string user)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                if (INTEXDBC == null)
                {
                    return setReturn;
                }
                DataRow dwData = SetEnv.ICC_WFEVENTSGROUP.NewRow();
                dwData["MANDANT"] = Mandant;
                setResult = INTEXDBC.QueryICC_WFEVENTSGROUP(dwData, 4);
                foreach (DataRow row in setResult.Tables[0].Rows)
                {
                    row["WFTDES"] = Convert8859P1ToGB2312(row["WFTDES"].ToString());
                }
                setReturn.Merge(setResult);
                dwData = SetEnv.ICC_WFEVENTSCLASS.NewRow();
                dwData["MANDANT"] = Mandant;
                setResult = INTEXDBC.QueryICC_WFEVENTSCLASS(dwData, 4);
                foreach (DataRow row in setResult.Tables[0].Rows)
                {
                    row["WFCDES"] = Convert8859P1ToGB2312(row["WFCDES"].ToString());
                }
                setReturn.Merge(setResult);
                dwData = SetEnv.ICC_FUNCTIONS.NewRow();
                dwData["MANDANT"] = Mandant;
                setResult = INTEXDBC.QueryICC_FUNCTIONS(dwData, 9999);
                foreach (DataRow row in setResult.Tables[0].Rows)
                {
                    row["DESCRIPTION"] = Convert8859P1ToGB2312(row["DESCRIPTION"].ToString());
                }
                setReturn.Merge(setResult);
                dwData = SetEnv.ICC_WFSUBSCRIBER.NewRow();
                dwData["MANDANT"] = Mandant;
                dwData["WFEUSER"] =Left( user.PadRight(8),8);
                setResult = INTEXDBC.QueryICC_WFEVENTDEF(dwData, 3);
                foreach (DataRow row in setResult.Tables[0].Rows)
                {
                    row["WFEDES"] = Convert8859P1ToGB2312(row["WFEDES"].ToString());
                }
                setReturn.Merge(setResult);
                dwData = SetEnv.HRSTAMM.NewRow();
                dwData["HRMANDANT"] = Mandant;
                setResult = INTEXDBC.QueryHRSTAMM(dwData, 4); 
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet Load(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwData = null;
            try
            {
            
                IsSetEnvUpdate=false;
                dwData = SetEnv.ICC_WFEVENTS.NewRow();
                dwData["MANDANT"] = dwInput["MANDANT"];
                dwData["WFEUSER"] = dwInput["WFEUSER"];
                dwData["WFSOURCE"] = dwInput["WFSOURCE"];                
                dwData["WFEVENTS"] = dwInput["WFEVENTS"];
                dwData["KRS"] = dwInput["KRS"];
                dwData["LFD"] = dwInput["LFD"];
                dwData["CONTROL"] = dwInput["CONTROL"];
                setResult = INTEXDBC.QueryICC_WFEVENTS(dwData, 2);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void Save()
        {
            try
            {
                foreach (DataRow rw in SetDetail.ICC_WFEVENTS.Rows)
                {
                    INTEXDBC.SaveICC_WFEVENTS(rw);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Delete(DataRow dwInput)
        {
            try
            {
                INTEXDBC.DeleteICC_WFEVENTS(dwInput, 1);
                DataRow[] rows;
                rows = SetMaster.ICC_WFEVENTS.Select("ROWNID = '" + dwInput["ROWNID"].ToString() + "'");
                foreach (DataRow rw in rows)
                {
                    rw.Delete();
                }
                rows = SetDetail.ICC_WFEVENTS.Select("ROWNID = '" + dwInput["ROWNID"].ToString() + "'");
                foreach (DataRow rw in rows)
                {
                    rw.Delete();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void DeleteSameSource(DataRow dwInput)
        {
            try
            {
                INTEXDBC.DeleteICC_WFEVENTS(dwInput, 2);
                DataRow[] rows;
                rows = SetMaster.ICC_WFEVENTS.Select("WHAT = '" + dwInput["WHAT"].ToString() + "'");
                foreach (DataRow rw in rows)
                {
                    rw.Delete();
                }
                /*rows = SetDetail.ICC_WFEVENTS.Select("WHAT = '" + dwInput["WHAT"].ToString() + "'");
                foreach (DataRow rw in rows)
                {
                    rw.Delete();
                }*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        
    }
}
